package demo1222.pm;

/*
try-catch...
try-catch...finally
try-finally

 */
public class Demo_05 {
    public static void main(String[] args) {
        System.out.println(sum(1));//100

        StringBuilder s = new StringBuilder();
        StringBuilder sum = sum(s);
        System.out.println(sum);//"hellotrue"
    }

    public static int sum(int a) {
        try {
            a = 100;
            /*
            在执行finally语句之前,return语句会把返回中的值存好,
            等finally语句执行完后,把存好的值返回.
             */
            return a;//100
        } finally {
            System.out.println("finally...");
            a = 200;
        }
    }


    //为什么StringBuilder在finally里改变值奏效，因为a.append直接操作地址，可以改变
    public static StringBuilder sum(StringBuilder a) {
        try {
            a.append("hello");
            return a;//a中的地址值
        } finally {
            System.out.println("finally...");
            a.append("true");
        }
    }


}
